﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _001_二分查找
{
    class Program
    {
        static void Main(string[] args)
        {
           /* int[] nums = { 8, 12, 21, 25, 29, 35, 74, 96 };
            int target = 12;*/
            Program p = new Program();
           // Console.WriteLine(p.Search(nums, target));

            int[] abc = { 1, 2, 3, 4, 5, 6, 7,8};
            


            p.Rotate(abc, 3);
            for (int i = 0; i < abc.Length; i++)
            {
                Console.WriteLine(abc[i]);
            }
            Console.ReadKey();

        }
        public int Search(int[] nums, int target)
        {
            //设最左边下标为0，最右边下标为数组长度-1
            int low = 0, high = nums.Length - 1;
            //如果左小于等于右，持续循环
            while (low <= high)
            {
                //求low到high的中间值；
                int mid = (high - low) / 2 + low;
                //把中间值赋值到num
                int num = nums[mid];
                //判断是否与target相等;
                if (num == target)
                {
                    //找到则返回数组下标
                    return mid;
                }
                //如果判断中间值比target要大，那就证明target在中间值的左边
                else if (num > target)
                {
                    //把high移动到中间值前一位，然后继续循环查找
                    high = mid - 1;
                }
                //如果判断中间值比target要小，那就证明target在中间值的右边
                else
                {
                    //把high移动到中间值后一位，然后继续循环查找
                    low = mid + 1;
                }
            }
            //上面的循环结束了还没有找到目标target的话，返回-1
            return -1;

        }

        public int SearchInsert(int[] nums, int target)
        {
            //设最左边下标为0，最右边下标为数组长度-1
            int low = 0, high = nums.Length - 1;
            //声明一个存储中间值的变量
            int mid = 0;
            //如果左小于等于右，持续循环
            while (low <= high)
            {
                //求low到high的中间值；
                mid = (high - low) / 2 + low;
                //判断是否与target相等;
                if (nums[mid] == target)
                {
                    //找到则返回数组下标
                    return mid;
                }
                //如果判断中间值比target要小，那就证明target在中间值的右边
                else if (nums[mid] < target)
                {
                    //把high移动到中间值后一位，然后继续循环查找
                    low = mid + 1;
                }
                //如果判断中间值比target要大，那就证明target在中间值的左边
                else
                {
                    //把high移动到中间值前一位，然后继续循环查找
                    high = mid - 1;
                }
            }

            //上面循环没有找到target，但中间值是最靠近target的数的。
            //判断相近数的大小，然后返回插入的下标
            if (target > nums[mid])
            {
                return mid + 1;
            }
            else
            {
                return mid;
            }

        }

        public void Rotate(int[] nums, int k)
        {
            int n = nums.Length;
            int[] arr = new int[n];
            for (int i = 0; i < k; i++)
            {
                arr[i] = nums[n - k + i];
            }
            for (int i = k; i < n; i++)
            {
                arr[i] = nums[i - k];
            }
            nums = arr;
           

            for (int i = 0; i < n; i++)
            {
                if (i == 0)
                {
                    Console.Write("[");
                }
                Console.Write(arr[i]);


                if (i == n - 1)
                {
                    Console.Write("]");
                }
                else
                {
                    Console.Write(",");
                }
            }

        }
    }

}
